package com.xu.easyexcel.controller;

import com.xu.easyexcel.commons.ResultObject;
import com.xu.easyexcel.commons.excel.ExcelUtils;
import com.xu.easyexcel.commons.util.IdWorker;
import com.xu.easyexcel.pojo.RlPointinfo;
import com.xu.easyexcel.pojo.User;
import com.xu.easyexcel.pojo.excel.RlPointinfoExcelModel;
import com.xu.easyexcel.pojo.excel.UserExcelModel;
import com.xu.easyexcel.service.IUserService;
import com.xu.easyexcel.service.RlPointinfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 整改建议详情列表详情列表 前端控制器
 * </p>
 *
 * @author RonnieXu
 * @since 2019-08-29
 */
@Slf4j
@Transactional(rollbackFor = Exception.class)
@Api(value = "RlPointinfo", tags = "RlPointinfo接口")
@RestController
@RequestMapping("/rlPointinfo")
public class RlPointinfoController {

    @Autowired
    private IUserService iUserService;

    @Autowired
    private IdWorker idWorker;

    @ApiOperation(value = "导入Excel表格", notes = "导入Excel表格")
    @RequestMapping(value = "/readExcel", method = RequestMethod.GET)
    public ResultObject readExcelModel() {
        try{

            FileInputStream inputStream = new FileInputStream("C:\\Users\\thh\\Desktop\\test.xlsx");
            List<UserExcelModel> list = ExcelUtils.readExcel(new BufferedInputStream(inputStream), UserExcelModel.class);

            List<User> userList = new ArrayList<>();
            System.out.println(userList);

            list.forEach(
                    demo->{

                       // User user = new User(idWorker.nextId(),
                        User user = new User(null,
                        demo.getUserName(),
                        demo.getPassword(),
                        demo.getRealName(),
                        demo.getPhone(),
                        demo.getEmail(),
                        null);

                userList.add(user);
            });



            System.out.println(list.size());
            //将表格数据保存到数据库
            iUserService.saveOrUpdateBatch(userList);
            return ResultObject.success();
        } catch (IOException e) {
            e.printStackTrace();
            return ResultObject.failure();
        }
    }

    @Autowired
    RlPointinfoService rlPointinfoService;

    @ApiOperation(value = "导出Excel表格", notes = "导出Excel表格")
    @RequestMapping(value = "/exprotExcel", method = RequestMethod.GET)
    public ResponseEntity exprotExcel() {


        try {
            List<RlPointinfo> list = rlPointinfoService.list();
            List<RlPointinfoExcelModel> dataList = new ArrayList<>();
            list.forEach(rlPointinfo->{
                RlPointinfoExcelModel rlPointinfoExcelModel = new RlPointinfoExcelModel();
                BeanUtils.copyProperties(rlPointinfo,rlPointinfoExcelModel);
                dataList.add(rlPointinfoExcelModel);
            });

            //数据写入到字节流
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            boolean flag = ExcelUtils.writeExcel(bos, RlPointinfoExcelModel.class, dataList);

            //下载文件
            String fileName = "pintInfo.xlsx";
            log.info("开始下载导出的Excel文件");
            return ExcelUtils.downloadExcel(fileName, bos);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @ApiOperation(value = "保存和修改", notes = "保存和修改")
    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    public ResultObject saveOrUpdate(@RequestBody @Validated User user) {
        boolean b = iUserService.saveOrUpdate(user);
        if(!b){
            return ResultObject.failure();
        }else
            return ResultObject.success();

    }
}
